package client.GUI.order;

import javax.swing.*;

import client.GUI.GUIClient;
import client.GUI.GUIConstants;
import client.GUI.dashboard.OpenOrderPanel;
import client.GUI.dashboard.PriceHistoryPanel;
import client.backend.Connection;
import client.backend.price.PriceController;
import client.backend.user.UserInformation;

import java.awt.Color;
import java.awt.event.*;
import java.io.IOException;


/**
 * Brings up the new order screen, allows the user to create a new trailing stop order.
 */
public class TrailingStop extends JPanel implements ActionListener {

	private static final long serialVersionUID = 1L;
		
	private JButton create, back, seePriceHistory;	
	private JComboBox currPairs, buySell;
	private JTextField ratio, enterAmount, enterPercent;
	
	/**
	 * This is the default constructor
	 */
	public TrailingStop() {
		super();
		initialize();
				
		updateRatio();
	}
	
	/**
	 * Sets the ratio of currencies to the currencies
	 * provided in currPair
	 * 
	 * @param currPair The Pair of currencies that will be assigned to ratio
	 */
	public TrailingStop(String currPair) {
		this();

		currPairs.setSelectedItem(currPair);
	}
	
	/**
	 * Sets the ratio of currencies to the currencies
	 * provided in currPair and sets the Buy/Sell
	 * option to the option provided in bS
	 * 
	 * @param currPair 	- The Pair of currencies that will be assigned to ratio
	 * @param bS		- The Buy/Sell option
	 * 
	 */
	public TrailingStop(Object currPair, Object bS, String amount) {
		this();
		
		currPairs.setSelectedItem(currPair);
		currPairs.setSelectedItem(bS);
		enterAmount.setText(amount);
	}
	
	/**
	 * Creates all the JLabels, JButtons, JTextFields and JComboBoxes,
	 * lays them out and sizes them properly. 
	 * 
	 * @return void
	 */
	private void initialize() {
		JLabel title = new JLabel("Create New Trailing Stop Order");
		JLabel selectCurr = new JLabel("Select the currencies you wish to trade:");
		JLabel currRatio = new JLabel("Current ratio:");
		JLabel amount = new JLabel("Enter amount of money you wish to trade: ");
		JLabel trailPercent = new JLabel("Trailing Percent (x.xx)%: ");
			
		currPairs = new JComboBox(PriceController.getInstance().getCurrPairs());		//Selects the currency pairs the client will trade with
		buySell = new JComboBox(GUIConstants.buySell);		//
		
		ratio = new JTextField();
		ratio.setEditable(false);
		enterAmount = new JTextField();
		enterPercent = new JTextField();
		
		seePriceHistory = new JButton("Price History");
		back = new JButton("Back");
		create = new JButton("Create Order");
		
		this.setBackground(Color.black);
		title.setForeground(Color.white);
		selectCurr.setForeground(Color.white);
		currRatio.setForeground(Color.white);
		amount.setForeground(Color.white);
		trailPercent.setForeground(Color.white);
			
		title.setBounds(255, 50, 142, 40);
		selectCurr.setBounds(90, 190, 230, 30);
		currPairs.setBounds(370, 190, 96, 30);
		buySell.setBounds(500, 190, 50, 30);
		seePriceHistory.setBounds(600, 190, 150, 30);
		currRatio.setBounds(90, 250, 77, 30);
		ratio.setBounds(184, 250, 100, 30);
		amount.setBounds(90, 350, 240, 30);
		enterAmount.setBounds(370, 350, 50, 30);
		trailPercent.setBounds(90, 410, 240, 30);
		enterPercent.setBounds(370, 410, 50, 30);
		create.setBounds(90, 550, 120, 30);
		back.setBounds(370, 550, 120, 30);
		
		/*I reduced the size of the panel by 50 in order to make room for the bottom panel*/
		this.setSize(1024, 718);
		this.setLayout(null);
		
		currPairs.addActionListener(this);
		buySell.addActionListener(this);
		back.addActionListener(this);
		create.addActionListener(this);
		seePriceHistory.addActionListener(this);
		
		this.add(title);
		this.add(selectCurr);
		this.add(currPairs);
		this.add(buySell);
		this.add(seePriceHistory);
		this.add(ratio);
		this.add(currRatio);
		this.add(amount);
		this.add(enterAmount);
		this.add(trailPercent);
		this.add(enterPercent);
		this.add(create);
		this.add(back);
	}
	
	/**
	 * When an action is performed, it checks where it came from
	 * and then performs the appropriate action
	 * @param e the action source
	 */
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == create) {
			createTS();
		} else if (e.getSource() == seePriceHistory){
			showPriceHistory();
		} else if (e.getSource() == back) {
			GUIClient.getInstance().backFromTrailingStop();
		} else if ((e.getSource() == buySell) || (e.getSource() == currPairs)) {
			updateRatio();
		}
	}
	
	/**
	 * Opens a pop-up showing the price history of the selected price ratio
	 */
	private void showPriceHistory() {
		int bS = 2;
		if(buySell.getSelectedItem() == "Buy"){
			bS = 1;
		}
		PriceHistoryPanel prices;
		try {
			prices = new PriceHistoryPanel(currPairs.getSelectedItem().toString(), bS);
			prices.pack();
			prices.setVisible(true);
		} catch (IOException e1) {
			GUIClient.getInstance().popupFail("Could not get price history");
		}
	}
	
	/**
	 * If the client selected "Sell", it swaps curr1 and curr2 's data
	 * in order to keep curr1 as having the bought currency
	 */
	public void updateRatio() {
		String curr1 = currPairs.getSelectedItem().toString().substring(0,3);
		String curr2 = currPairs.getSelectedItem().toString().substring(3,6);
		
		if (buySell.getSelectedItem().toString().equals("Buy")) 
			ratio.setText(PriceController.getInstance().getPrice(curr1, curr2).getBuy()+"");
		else
			ratio.setText(PriceController.getInstance().getPrice(curr1, curr2).getSell()+"");
	}
	
	/**
	 * Creates the trailing stop order and sets all variables for order
	 */
	public void createTS() {
		String currPair = currPairs.getSelectedItem().toString();
		String amount = enterAmount.getText();
		String buySell = this.buySell.getSelectedItem().toString();
		String price = ratio.getText();
		String percent = enterPercent.getText();
		
		if (UserInformation.getInstance().getUser().createTS(currPair, buySell, amount, price, percent))
			GUIClient.getInstance().popupTS(currPair, buySell, amount, percent);
		else
			GUIClient.getInstance().popupFail();
		
		GUIClient.getInstance().loggedIn();
	}
	
}  //  @jve:decl-index=0:visual-constraint="82,22"



